package com.itheima.springbootwebtlias.controller;


import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.util.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload( MultipartFile file) throws Exception {
        log.info("文件上传，参数：{} {}",file);

        //1.接受请求参数，接收文件，MultipartFile

        //2.使用工具类，实现文件上传到阿里云服务器
        String imagePath = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());

        return Result.success(imagePath);
    }
}
